package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.controller.BaseController;
import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzPagination;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.domain.RmpNotice;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.services.RmpNoticeService;

@Controller
public class RmpNoticeController extends BaseController {
	@Autowired
	private RmpNoticeService rmpNoticeService;
	@Autowired
	@Qualifier("rmpOpLogService")
	private OpLogService opLogService;

	@RequiresPermissions("notice:list")
	@RequestMapping(value = "/system/notice/list")
	public String list(HttpServletRequest request) {
		opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_NOTICE.toString());
		rmpNoticeService.dwzFindAll((DwzPagination) request.getAttribute(DwzPagination.DWZPAGINATION_PROPERTY_KEY));
		return "system/notice/list";
	}

	@RequestMapping(value = "/system/notice/add", method = RequestMethod.GET)
	public String toAdd() {
		return "system/notice/edit";
	}

	@RequiresPermissions("notice:add")
	@RequestMapping(value = "/system/notice/add", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse add(@Valid RmpNotice notice, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return DwzResponseUtils.createFailResponse(bindingResult.getAllErrors());
		} else {
			RmpNotice newRmpNotice = rmpNoticeService.save(notice);
			opLogService.addLog(RmpOpLogTypeEnum.ADD.toString(), RmpOpLogBusiTypeEnum.RMP_NOTICE.toString(),
					newRmpNotice.getId(), newRmpNotice.getTitle());
			return DwzResponseUtils.createSuccessResponse(true, "notice_panel");
		}
	}

	@RequestMapping(value = "/system/notice/edit/{id}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("id") String id, ModelMap model) {
		RmpNotice notice = rmpNoticeService.get(id);
		model.put("notice", notice);

		return "system/notice/edit";
	}

	@RequiresPermissions("notice:view")
	@RequestMapping(value = "/system/notice/view/{id}", method = RequestMethod.GET)
	public String toView(@PathVariable("id") String id, ModelMap model) {
		RmpNotice notice = rmpNoticeService.get(id);
		opLogService.addLog(RmpOpLogTypeEnum.VIEW.toString(), RmpOpLogBusiTypeEnum.RMP_NOTICE.toString(),
				notice.getId(), notice.getTitle());
		model.put("notice", notice);
		model.put("readOnly", "1");

		return "system/notice/edit";
	}

	@RequiresPermissions("notice:edit")
	@RequestMapping(value = "/system/notice/edit", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse edit(RmpNotice notice) {
		rmpNoticeService.update(notice);
		opLogService.addLog(RmpOpLogTypeEnum.UPDATE.toString(), RmpOpLogBusiTypeEnum.RMP_NOTICE.toString(),
				notice.getId(), notice.getTitle());

		return DwzResponseUtils.createSuccessResponse(true, "notice_panel");
	}

	@RequiresPermissions("notice:delete")
	@RequestMapping(value = "/system/notice/delete", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			List<String> idList = new ArrayList<String>();
			for (String id : ids.split(",")) {
				if (StringUtils.isNotBlank(id)) {
					idList.add(StringUtils.trim(id));
				}
			}
			rmpNoticeService.delete(idList);

			for (String id : idList) {
				opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_NOTICE.toString(), id);
			}
		}
		return DwzResponseUtils.createSuccessResponse(false);
	}
}
